class Solution {
 public:
  ListNode* reverseBetween(ListNode* head, int left, int right) {
    vector<int> ans;
    left-=1;
    right-=1;
    ListNode* cur = head;
    //先写进left
    while (cur) {
      ans.push_back(cur->val);
      cur = cur->next;
    }
    //交换
    while (left < right) {
      swap(ans[left++], ans[right--]);
    }
    cur = head;
    for (int i = 0; i < ans.size(); ++i) {
      cur->val = ans[i];
      cur = cur->next;
    }
    return head;
  }
};
